<?php
$host = @$_POST['host'] ;
$username = @$_POST['username'] ;
$password = @$_POST['password'] ;
$databaseName  = @$_POST['databaseName'] ;
$Year = @$_POST['year'] ;

$matk = @$_POST['matk'] ;
$mant = @$_POST['mant'] ;
$dknovn = @$_POST['dknovn'] ;
$dknont = @$_POST['dknont'] ;
$mode = @$_POST['mode'] ;

$link = mysqli_connect($host,$username,$password,$databaseName) or die("Error " . mysqli_error($link));

if (!mysqli_set_charset($link, "utf8")) {
} else {
}

$sql = "select * from SDTT".$Year ;
$checktable = mysqli_query($link, $sql); 
if($checktable === FALSE)
{
		echo "1";
		return;
}

$succ = FALSE;
if ($mode == "add")
{
	$sql = "select * from SDTT".$Year." where matk = '".$matk."' && mant = '".$mant."'  LIMIT 1" ;
	$checktable = mysqli_query($link, $sql);
	if ($checktable->num_rows ==0)
		$succ = true;

}
else
{
	$oldmatk = @$_POST['oldmatk'] ;
	$oldmant = @$_POST['oldmant'] ;
	if ($matk == $oldmatk  && $mant == $oldmant) //ok
	{
		$succ = true;
	}
	else
	{
		$sql = "select * from SDTT".$Year." where matk = '".$matk."'  && mant = '".$mant."' LIMIT 1" ;
		$checktable = mysqli_query($link, $sql);
		if ($checktable->num_rows ==0)
			$succ = true;

	}
}

if($succ == true)
{
}
else
{
	echo "2";
	return;
}


if ($mode == "add")
{
	$mysql = "INSERT INTO SDTT".$Year."  SET matk='". $matk. "',";
	$mysql .= "  mant='". $mant. "', ";
	$mysql .= "  dknovn='". $dknovn. "', ";
	$mysql .= "  dknont='". $dknont. "' ";
}
else
{
	$mysql = "UPDATE SDTT".$Year."  SET matk='". $matk. "',";
	$mysql .= "  mant='". $mant. "', ";
	$mysql .= "  dknovn='". $dknovn. "', ";
	$mysql .= "  dknont='". $dknont. "' ";	
	$mysql .= "  where matk ='". $oldmatk. "' && mant ='".$oldmant."' LIMIT 1 ";
}

//echo $mysql;
$succ = mysqli_query($link, $mysql);
if ($succ == false)
{
	echo "3";
	return;
}


mysqli_close($link);


?> 

